package cn.fl.collection.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.IPriorityApiService;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.condition.SysUsrFunAuthRQueryCondition;
import cmm.user.facade.system.condition.SysUsrQueryCondition;
import cmm.user.facade.system.dto.SysOrgDTO;
import cmm.user.facade.system.dto.SysUsrDTO;
import cmm.user.facade.system.dto.SysUsrFunAuthRDTO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.*;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.collection.condition.RskCltAuthQC;
import cn.fl.collection.constant.CollectionConstant;
import cn.fl.collection.dto.RskCltAuthDTO;
import cn.fl.collection.service.IRskCltAuthService;
import cn.fl.collection.transform.RskCltAuthVOTransform;
import cn.fl.collection.vo.RskCltAuthVO;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.customer.service.ICstCorpTeamService;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.customer.vo.CstCorpTeamVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.condition.PrjPrdPaySchDQueryCondition;
import cn.fl.project.service.IPrjPrdPaySchDService;
import cn.fl.project.vo.PrjPrdPaySchDVO;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysDictionaryService;
import com.alibaba.fastjson.JSONObject;
import fl.constant.facade.framework.constant.DictionaryConstant;
import fl.customer.facade.customer.condition.CstCorpTeamQueryCondition;
import fl.customer.facade.customer.constant.CustomerConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 委外催收表 控制层
 */
@Controller
@RequestMapping("/clt/auth/rskCltAuth")
public class RskCltAuthController extends BaseController {


	/**
	 * RskCltAuth 服务层BEAN
	 */
	@Autowired
	private IRskCltAuthService rskCltAuthService;

	/**
	 * 系统用户 API 暴露服务
	 */
	@Autowired
	private ISysUsrApiService sysUsrApiService;

	/**
	 * CstBscInfo 服务层BEAN
	 */
	@Autowired
	private ICstBscInfoService cstBscInfoService;

	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryService sysDictionaryService;

	@Autowired
	private ICstCorpTeamService cstCorpTeamService;

	@Autowired
	private IPrjPrdPaySchDService prjPrdPaySchDService;

	@Autowired
	private ISysOrgApiService sysOrgApiService;

	/**
	 *  权限暴露服务
	 */
	@Autowired
	private IPriorityApiService priorityApiService;

	/**
	 * 到委外分案列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("collection/rskcltauth/rskCltAuthListInfo");
		BaseLogUtils.newLogger("到委外分案列表页面.", CurrentThreadContext.getCurrentUserName()).setKeys("toListPage").info();
		return view;
	}

	/**
	 * 到委外催收列表页面
	 */
	@RequestMapping(value = "/toRskCltAuthCollectionListPage.do", method = { RequestMethod.GET})
	public ModelAndView toRskCltAuthCollectionListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("collection/rskcltauth/rskCltAuthCollectionListInfo");
		String showAuthOrg = "0";
		List<SysUsrDTO> sysUsrDTOS = new ArrayList<>();
		//是否显示委外催收页面的委案机构搜索
		if(CollectionConstant.THIRD_PARTY_COLLECTION_ORG_CD.equals(CurrentThreadContext.getCurrentOrgCd())){
			//当前用户为第三方机构用户
			showAuthOrg = "1";
		}else{
			SysOrgDTO sysOrgDTO = sysOrgApiService.selectSysOrgByOrgCd(CollectionConstant.THIRD_PARTY_COLLECTION_ORG_CD,CurrentThreadContext.getCurrentSysCd());
			Pager page = new Pager();
			page.setEveryPage(200);
			page.setCurrentPage(1);
			SysUsrQueryCondition qc = new SysUsrQueryCondition();
			qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
			qc.setOrgId(CurrentThreadContext.getCurrentOrgId());
			qc.setOrgId(sysOrgDTO.getId());
			page.setCondition(qc);
			Pager pager = this.sysUsrApiService.searchFunAuthUsrByOrgId(page);
			sysUsrDTOS = (List<SysUsrDTO>)pager.getResultList();
		}
		BaseLogUtils.newLogger("到委外催收列表页面.", CurrentThreadContext.getCurrentUserName()).setKeys("toRskCltAuthCollectionListPage").info();
		view.addObject("showAuthOrg",showAuthOrg);
		view.addObject("authOrgList",sysUsrDTOS);
		return view;
	}


	/**
	 * 到委外催收分案页面
	 */
	@RequestMapping(value = "/toRskCltAuthPage.do", method = { RequestMethod.GET})
	public ModelAndView toRskCltAuthPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("collection/rskcltauth/rskCltAuthDtlListInfo");
		String paySchId = request.getParameter("paySchId");
		String cstId = request.getParameter("cstId");

		//查询之前是否有委案中的委外记录
		RskCltAuthQC rskCltAuthQC = new RskCltAuthQC();
		//rskCltAuthQC.setPaySchId(Long.valueOf(paySchId));
		rskCltAuthQC.setCstId(Long.valueOf(cstId));
		rskCltAuthQC.setAuthStsCd(0);
		List<RskCltAuthVO> rskCltAuthVOS = this.rskCltAuthService.searchList(rskCltAuthQC);
		List<SysUsrDTO> sysUsrDTOS = new ArrayList<>();
		SysOrgDTO sysOrgDTO = sysOrgApiService.selectSysOrgByOrgCd(CollectionConstant.THIRD_PARTY_COLLECTION_ORG_CD,CurrentThreadContext.getCurrentSysCd());
		Pager page = new Pager();
		page.setEveryPage(200);
		page.setCurrentPage(1);
		SysUsrQueryCondition qc = new SysUsrQueryCondition();
		qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
		qc.setOrgId(CurrentThreadContext.getCurrentOrgId());
		qc.setOrgId(sysOrgDTO.getId());
		page.setCondition(qc);
		Pager pager = this.sysUsrApiService.searchFunAuthUsrByOrgId(page);
		sysUsrDTOS = (List<SysUsrDTO>)pager.getResultList();

		if(CollectionUtils.isNotEmpty(rskCltAuthVOS)){
			SysUsrDTO sysUsrDTO = this.sysUsrApiService.selectSysUsrById(Long.valueOf(rskCltAuthVOS.get(0).getAuthOrgCd()),CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
			if(null != sysUsrDTO){
				SysUsrDTO sysUsrDTO1 = this.sysUsrApiService.selectSysUsrWithAuthBy(CurrentThreadContext.getCurrentSysCd(),sysUsrDTO.getUsrNm());
				List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOList = sysUsrDTO1.getSysUsrFunAuthRDTOList();
				if(CollectionUtils.isNotEmpty(sysUsrFunAuthRDTOList)){
					for(SysUsrFunAuthRDTO sysUsrFunAuthRDTO : sysUsrFunAuthRDTOList){
						if(CollectionConstant.THIRD_PARTY_COLLECTION_ORG_CD.equals(sysUsrFunAuthRDTO.getOrgCd())){
							sysUsrDTOS.clear();
							sysUsrDTOS.add(sysUsrDTO);
						}
					}
				}
			}
		}

		view.addObject("authOrgCdList", JSONObject.toJSONString(sysUsrDTOS));
		view.addObject("cstId", cstId);
		view.addObject("paySchId", paySchId);
		BaseLogUtils.newLogger("跳转到委外催收分案页面.", CurrentThreadContext.getCurrentUserName()).setKeys("toRskCltAuthPage").info();
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("/rskCltAuth/modify");
		view.addObject("id", id);
		BaseLogUtils.newLogger("跳转到修改页面.", CurrentThreadContext.getCurrentUserName()).setKeys("toModifyPage").info();
		return view;
	}


	/**
	 * 初始化委外分案列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String paySchNo = request.getParameter("paySchNo");
		String paySchId = request.getParameter("paySchId");
		String cstNm = request.getParameter("cstNm");
		String cstMgrOrgCd = request.getParameter("orgId");
		String minOverdueDayStr = request.getParameter("minOverdueDay");
		String maxOverdueDayStr = request.getParameter("maxOverdueDay");

		Pager page = new Pager();
		RskCltAuthQC qc = new RskCltAuthQC();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		if (StringUtils.isNotEmpty(paySchNo)) {
			qc.setPaySchNo(paySchNo);
		}
		if (StringUtils.isNotEmpty(cstNm)) {
			qc.setCstNm(cstNm);
		}
		if (StringUtils.isNotEmpty(cstMgrOrgCd)) {
			qc.setCstMgrOrgCd(cstMgrOrgCd);
		}
		if (StringUtils.isNotEmpty(minOverdueDayStr)) {
			qc.setMinOverdueDay(Integer.parseInt(minOverdueDayStr));
		}
		if (StringUtils.isNotEmpty(maxOverdueDayStr)) {
			qc.setMaxOverdueDay(Integer.parseInt(maxOverdueDayStr));
		}
		if (StringUtils.isNotEmpty(paySchId)) {
			qc.setPaySchId(Long.valueOf(paySchId));
		}
		//罚息时间取当天
		qc.setCreateDate(DateUtils.formatDate(new Date(),DateUtils.YYYY_MM_DD));
		page.setCondition(qc);

		//TODO SOMETHING
		page = this.rskCltAuthService.searchRskCltAuthListPage(page);
		List<RskCltAuthVO> list = (List<RskCltAuthVO>) page.getResultList();
		CstBscInfoVO cstBscInfoVO = null;
		if(CollectionUtils.isNotEmpty(list)){
			for(RskCltAuthVO rskCltAuthVO : list){
				//查询用户类型
				cstBscInfoVO = cstBscInfoService.selectById(rskCltAuthVO.getCstId());
				if(null != cstBscInfoVO){
					rskCltAuthVO.setCstTypCd(cstBscInfoVO.getCstTypCd());
				}
			}
		}
		ListPageVO<RskCltAuthVO> listPage = new ListPageVO<RskCltAuthVO>(list, page.getTotalCount());
		return listPage;
	}





	/**
	 * 初始化委外分案列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initRskCltAuthList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initRskCltAuthList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String paySchId = request.getParameter("paySchId");
		String buOrgCd = request.getParameter("buOrgCd");

		Pager page = new Pager();
		RskCltAuthQC qc = new RskCltAuthQC();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		if (StringUtils.isNotEmpty(paySchId)) {
			qc.setPaySchId(Long.valueOf(paySchId));
		}
		if (StringUtils.isNotEmpty(buOrgCd)) {
			qc.setBuOrgCd(buOrgCd);
		}
		page.setCondition(qc);

		//TODO SOMETHING
		page = this.rskCltAuthService.searchListPage(page);
		List<RskCltAuthVO> list = RskCltAuthVOTransform.toVOList((List<RskCltAuthDTO>) page.getResultList());
		if(CollectionUtils.isNotEmpty(list)){
			for(RskCltAuthVO rskCltAuthVO : list){
				SysUsrDTO sysUsrDTO = this.sysUsrApiService.selectSysUsrById(Long.valueOf(rskCltAuthVO.getAuthOrgCd()),CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
				if(null != sysUsrDTO){
					rskCltAuthVO.setAuthOrgCdNm(sysUsrDTO.getRefRlNm());
				}

			}
		}
		ListPageVO<RskCltAuthVO> listPage = new ListPageVO<RskCltAuthVO>(list, page.getTotalCount());
		return listPage;
	}


	/**
	 * 初始化委外催收列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initRskCltAuthCollectionList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initRskCltAuthCollectionList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String paySchNo = request.getParameter("paySchNo");
		String cstNm = request.getParameter("cstNm");
		String startDateStr = request.getParameter("startDate");
		String endDateStr = request.getParameter("endDate");
		String minOverdueDayStr = request.getParameter("minOverdueDay");
		String maxOverdueDayStr = request.getParameter("maxOverdueDay");
		String authOrgCd = request.getParameter("authOrgCd");

		Pager page = new Pager();
		RskCltAuthQC qc = new RskCltAuthQC();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		if (StringUtils.isNotEmpty(paySchNo)) {
			qc.setPaySchNo(paySchNo);
		}
		if (StringUtils.isNotEmpty(cstNm)) {
			qc.setCstNm(cstNm);
		}
		if (StringUtils.isNotEmpty(minOverdueDayStr)) {
			qc.setMinOverdueDay(Integer.parseInt(minOverdueDayStr));
		}
		if (StringUtils.isNotEmpty(maxOverdueDayStr)) {
			qc.setMaxOverdueDay(Integer.parseInt(maxOverdueDayStr));
		}
		if (StringUtils.isNotEmpty(startDateStr)) {
			qc.setStartDate(DateUtils.formatStringToDate(startDateStr,DateUtils.YYYY_MM_DD));
		}
		if (StringUtils.isNotEmpty(endDateStr)) {
			qc.setEndDate(DateUtils.formatStringToDate(endDateStr,DateUtils.YYYY_MM_DD));
		}
		if (StringUtils.isNotEmpty(authOrgCd)) {
			qc.setAuthOrgCd(authOrgCd);
		}
		page.setCondition(qc);
		if(CollectionConstant.THIRD_PARTY_COLLECTION_ORG_CD.equals(CurrentThreadContext.getCurrentOrgCd())){
			//当前用户为第三方机构用户
			qc.setAuthOrgCd(String.valueOf(CurrentThreadContext.getCurrentUserId()));
		}
		//TODO SOMETHING
		page = this.rskCltAuthService.searchRskCltAuthCollectionListPage(page);
		List<RskCltAuthVO> list = (List<RskCltAuthVO>) page.getResultList();
		CstBscInfoVO cstBscInfoVO = null;
		SysUsrDTO sysUsrDTO = null;
		if(CollectionUtils.isNotEmpty(list)){
			for(RskCltAuthVO rskCltAuthVO : list){
				//查询委案机构
				sysUsrDTO = this.sysUsrApiService.selectSysUsrById(Long.valueOf(rskCltAuthVO.getAuthOrgCd()),CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
				if(null != sysUsrDTO){
					rskCltAuthVO.setAuthOrgCdNm(sysUsrDTO.getRefRlNm());
				}
				//查询用户类型
				cstBscInfoVO = cstBscInfoService.selectById(rskCltAuthVO.getCstId());
				if(null != cstBscInfoVO){
					rskCltAuthVO.setCstTypCd(cstBscInfoVO.getCstTypCd());
				}
			}
		}
		ListPageVO<RskCltAuthVO> listPage = new ListPageVO<RskCltAuthVO>(list, page.getTotalCount());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.rskCltAuthService.removeById(Long.valueOf(id));
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
		}

		return result;
	}

	/**
	 * 得到一条委外记录
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getRskCltAuthByPaySchId.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getRskCltAuthByPaySchId(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String paySchId = request.getParameter("paySchId");
		String cstId = request.getParameter("cstId");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(paySchId)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		String cstNm = request.getParameter("cstNm");
		Pager page = new Pager();
		RskCltAuthQC qc = new RskCltAuthQC();
		page.setCurrentPage(1);
		page.setEveryPage(1);
		if (StringUtils.isNotEmpty(cstNm)) {
			qc.setCstNm(cstNm);
		}
		if (StringUtils.isNotEmpty(paySchId)) {
			qc.setPaySchId(Long.valueOf(paySchId));
		}//罚息时间取当天
		qc.setCreateDate(DateUtils.formatDate(new Date(),DateUtils.YYYY_MM_DD));
		page.setCondition(qc);

		try {
			//TODO SOMETHING
			RskCltAuthQC rskCltAuthQC = new RskCltAuthQC();
			rskCltAuthQC.setPaySchId(Long.valueOf(paySchId));
			rskCltAuthQC.setCstId(Long.valueOf(cstId));
			RskCltAuthVO rskCltAuthVO = this.rskCltAuthService.selectRskCltAuthByPaySchId(rskCltAuthQC);
			rskCltAuthVO.setAuthDaysStr("请选择");
			rskCltAuthVO.setAuthOrgCdNm("请选择");
			if(StringUtils.isNotEmpty(rskCltAuthVO.getAuthOrgCd())){
				SysUsrDTO sysUsrDTO = this.sysUsrApiService.selectSysUsrById(Long.valueOf(rskCltAuthVO.getAuthOrgCd()),CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
				rskCltAuthVO.setAuthOrgCd(null);
				rskCltAuthVO.setAuthOrgCdNm("请选择");
				if(null != sysUsrDTO){
					SysUsrFunAuthRQueryCondition sysUsrFunAuthRQueryCondition = new SysUsrFunAuthRQueryCondition();
					sysUsrFunAuthRQueryCondition.setUsrId(sysUsrDTO.getId());
					List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOS  = this.priorityApiService.searchSysUsrFunAuthRDTOList(sysUsrFunAuthRQueryCondition);
					SysOrgDTO sysOrgDTO = sysOrgApiService.selectById(sysUsrFunAuthRDTOS.get(0).getOrgId());
					if(null != sysOrgDTO){
						if(CollectionConstant.THIRD_PARTY_COLLECTION_ORG_CD.equals(sysOrgDTO.getOrgCd())){
							rskCltAuthVO.setAuthOrgCdNm(sysUsrDTO.getRefRlNm());
							rskCltAuthVO.setAuthOrgCd(String.valueOf(sysUsrDTO.getId()));
						}
					}
				}
			}
			List<RskCltAuthVO> list = new ArrayList<>();
			list.add(rskCltAuthVO);
			ListPageVO<RskCltAuthVO> listPage = new ListPageVO<RskCltAuthVO>(list,1);
			return listPage;
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.newLogger("获取委案记录异常").setKeys("getRskCltAuthByPaySchId").setThrowable(ex).error(ex.getMessage());
		}catch(Exception e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.newLogger("获取委案记录异常").setKeys("getRskCltAuthByPaySchId").setThrowable(e).error(e.getMessage());

		}
		return result;
	}

	/**
	 * 得到一条记录
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}

		try {
			RskCltAuthVO vo = this.rskCltAuthService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
			}
			result.setData(vo);
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String cstNm = request.getParameter("cstNm");
		String cstId = request.getParameter("cstId");
		String paySchId = request.getParameter("paySchId");
		String paySchNo = request.getParameter("paySchNo");
		String authDays = request.getParameter("authDays");
		String authOrgCd = request.getParameter("authOrgCd");
		String breachAmt = request.getParameter("breachAmt");
		String authAmt = request.getParameter("authAmt");
		//委案状态
		String authStsCd = request.getParameter("authStsCd");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		RskCltAuthVO entity = new RskCltAuthVO();
		try {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("委案机构不存在");
			SysUsrDTO sysUsrDTO = this.sysUsrApiService.selectSysUsrById(Long.valueOf(authOrgCd),CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
			if(sysUsrDTO == null){
				return result;
			}
			SysUsrFunAuthRQueryCondition sysUsrFunAuthRQueryCondition = new SysUsrFunAuthRQueryCondition();
			sysUsrFunAuthRQueryCondition.setUsrId(Long.valueOf(authOrgCd));
			List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOS  = this.priorityApiService.searchSysUsrFunAuthRDTOList(sysUsrFunAuthRQueryCondition);
			if(CollectionUtils.isNotEmpty(sysUsrFunAuthRDTOS)){
				SysOrgDTO sysOrgDTO = sysOrgApiService.selectById(sysUsrFunAuthRDTOS.get(0).getOrgId());
				if(null != sysOrgDTO){
					if(sysOrgDTO.getOrgCd().equals(CollectionConstant.THIRD_PARTY_COLLECTION_ORG_CD)){
						entity.setStartDate(new Date());
						entity.setEndDate(DateUtils.getDateAddDate(new Date(),5, Integer.valueOf(authDays)));
						entity.setPaySchId(Long.valueOf(paySchId));
						entity.setPaySchNo(paySchNo);
						entity.setCstId(Long.valueOf(cstId));
						entity.setCstNm(cstNm);
						entity.setAuthOrgCd(authOrgCd);
						entity.setAuthAmt(Long.valueOf(authAmt));
						entity.setBreachAmt(Long.valueOf(breachAmt));
						entity.setAuthDays(Integer.valueOf(authDays));
						if(StringUtils.isNotEmpty(authStsCd)){
							entity.setAuthStsCd(Integer.valueOf(authStsCd));
						}
						entity.setCrtUsrNm(CurrentThreadContext.getCurrentUserName());
						entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
						entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
						entity.setMdfUsrNm(CurrentThreadContext.getCurrentUserName());
						//验证数据
						result = this.rskCltAuthService.verifyRskCltAuth(entity,result);
						if(result.getSuccess() == BaseConstant.IS_YESNO_YES){
							Long id = this.rskCltAuthService.add(entity);
							if(null != id){
								result.setSuccess(BaseConstant.IS_YESNO_YES);
								result.setInfo("委外分案数据添加成功");
								result.setData(id);
								BaseLogUtils.newLogger("新增数据成功.").setKeys("add","entity="+JSONObject.toJSONString(entity)).info();
							}else{
								result.setSuccess(BaseConstant.IS_YESNO_NO);
								result.setInfo("委外分案数据添加失败");
							}
						}
					}
				}
			}

		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.newLogger("新增数据失败.").setKeys("add","entity="+JSONObject.toJSONString(entity)).setThrowable(ex).error(LogExceptionEnum.COMMON_CODE_0002);

		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String authStsCd = request.getParameter("authStsCd");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.newLogger("修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName()).setKeys("modify").info();
			return result;
		}
		RskCltAuthVO entity = null;
		try {
			entity = new RskCltAuthVO();
			entity.setId(Long.valueOf(id));
			entity.setAuthStsCd(Integer.valueOf(authStsCd));
			entity.setEndDate(new Date());
			//查询是否委案中
			RskCltAuthVO rskCltAuthVO = this.rskCltAuthService.selectById(Long.valueOf(id));
			if(rskCltAuthVO != null){
				if(0 != rskCltAuthVO.getAuthStsCd()){
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("委案数据状态不是委案中!");
					return result;
				}
			}
			boolean success = this.rskCltAuthService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.newLogger("修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName()).setKeys("modify").info();
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.newLogger("修改数据失败.").setKeys("modify","entity="+JSONObject.toJSONString(entity)).setThrowable(ex).error(LogExceptionEnum.COMMON_CODE_0002);
		}
		return result;
	}





	/**
	 * 到自然人详细页面
	 */
	@RequestMapping(value = "/toCustCstBacInfoNpDetailPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toCustCstBacInfoNpDetailPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("collection/cstbscinfo/custCstBacInfoNpDetail");
		view.addObject("id", id);
		BaseLogUtils.newLogger("跳转到自然人详情页面.", CurrentThreadContext.getCurrentUserName()).setKeys("toCustCstBacInfoNpDetailPage").info();
		return view;
	}

	/**
	 * 到企业详细页面
	 */
	@RequestMapping(value = "/toCustCstBacInfoCorpDetailPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toCustCstBacInfoCorpDetailPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("collection/cstbscinfo/custCstBacInfoCorpDetail");
		String roleIsEncrypt=this.sysDictionaryService.getRoleisEncrypt();

		CstCorpTeamQueryCondition cstCorpTeamQueryCondition = new CstCorpTeamQueryCondition();
		List<Long> cstIdList = new ArrayList<>();
		cstIdList.add(Long.valueOf(id));
		cstCorpTeamQueryCondition.setCstIdList(cstIdList);
		cstCorpTeamQueryCondition.setSysCd(CurrentThreadContext.getCurrentSysCd());
		List<CstCorpTeamVO> teamMenList = this.cstCorpTeamService.searchList(cstCorpTeamQueryCondition);
		if(CollectionUtils.isNotEmpty(teamMenList) && BaseConstant.IS_YESNO_YES_STR.equals(roleIsEncrypt)){
			for (CstCorpTeamVO vo:teamMenList){
				vo.setCertNo(EncryptUtil.certNoEncrypt(vo.getCertNo()));
				vo.setMp1(EncryptUtil.mobileEncrypt(vo.getMp1()));
			}
		}
		view.addObject("teamMenList", teamMenList);
		// 证件类型(自然人)
		List<KeyValueVO> certTypCdForNpList = this.cstBscInfoService.searchCertTypCdList(CustomerConstant.CST_TYP_CD_NP);
		view.addObject("searchSelectCertTypCdForNpList", certTypCdForNpList);
		List<String> codeGrpCdList = new ArrayList<>();
		codeGrpCdList.add(DictionaryConstant.CODE_GRP_M_TYP_CD);
		Map<String, List<KeyValueVO>> dicMap = this.sysDictionaryService.getMultiKvList(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
		view.addObject("searchSelectMTypCdList", dicMap.get(DictionaryConstant.CODE_GRP_M_TYP_CD));
		view.addObject("id", id);
		BaseLogUtils.newLogger("到企业详细页面.", CurrentThreadContext.getCurrentUserName()).setKeys("toCustCstBacInfoCorpDetailPage").info();
		return view;
	}



	/**
	 * 计算时间
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addTime.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object addTime(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String day = request.getParameter("day");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		int days = Integer.parseInt(day);
		Date resultDate = DateUtils.getDateAddDate(new Date(),5,days);
		result.setData(resultDate);
		return result;
	}

	/**
	 * 加载租金列表数据
	 * @param paySchId
	 * @return
	 */
	@RequestMapping("/loadRentList.do")
	@ResponseBody
	public ListPageVO loadRentList(Long paySchId) {
		ListPageVO res = null;
		try {
			if (paySchId == null) {
				throw new BaseException("请求参数：支付表ID不能为空！");
			}

			PrjPrdPaySchDQueryCondition condition = new PrjPrdPaySchDQueryCondition();
			condition.setFeeTypCd(CapitalConstant.CODE_FEE_TYP_CD_S01);// 租金
			condition.setPaySchId(paySchId);
			condition.setSortName("PAY_PD");
			condition.setSortOrder("ASC");
			List<PrjPrdPaySchDVO> list = prjPrdPaySchDService.searchList(condition);
			for (PrjPrdPaySchDVO prjPrdPaySchDVO : list) {
				prjPrdPaySchDVO.setFeeTypCdNm("租金");
			}
			res = new ListPageVO(list, list.size());
		} catch (Exception e) {
			BaseLogUtils.newLogger(paySchId + "委外催收加载租金列表数据异常cause：").setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
		}
		return res;
	}
}

